﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class BuildManager : MonoBehaviour {

    public TurretData laserTurretData;
    public TurretData MissileTurretData;
    public TurretData StandTurretData;

    public TurretData selectTurretData;

    private int money = 1000;

    public Text moneyText;

    public Animator MoneyAnimator;

	// Use this for initialization
	void Start () {
		
	}

    void ChangeMoney(int change = 0)
    {
        money += change;
        moneyText.text = "¥ " + money;
    }


	// Update is called once per frame
	void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            if (EventSystem.current.IsPointerOverGameObject() == false)
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                bool isCollider = Physics.Raycast(ray, out hit, 1000, LayerMask.GetMask("mapcube"));
                if (isCollider)
                {
                    MapCube mapCube = hit.collider.GetComponent<MapCube>();
                    if (selectTurretData != null && mapCube.turretGo == null)
                    {
                        if (money > selectTurretData.cost)
                        {
                            ChangeMoney(-selectTurretData.cost);
                            mapCube.BuidTurret(selectTurretData.turretPrefab);
                        }
                        else
                        {
                            MoneyAnimator.SetTrigger("Flicker");
                        }
                    }
                    else if (mapCube.turretGo != null)
                    {

                    }
                }
            }
        }
	}

    public void OnLaserSelected(bool isOn)
    {
        if (isOn)
        {
            selectTurretData = laserTurretData;
            //Debug.Log("OnLaserSelected");
        }
    }
    public void OnMissileSelected(bool isOn)
    {
        if (isOn)
        {
            selectTurretData = MissileTurretData;
            //Debug.Log("OnMissileSelected");
        }
    }
    public void OnStandSelected(bool isOn)
    {
        if (isOn)
        {
            selectTurretData = StandTurretData;
            //Debug.Log("OnStandSelected");
        }
    }
}
